package com.javabasic2.paintserver;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class Question {

	private String answer;
	private int questionNo = 0;
	private ArrayList<String> questions;
	public Question() {
		questions = new ArrayList<String>();
		readFile();
		selectQuestion();
	}
	public String getAnswer() {
		return answer;
	}
	public boolean isCheck(String answer) {
		return this.answer.equals(answer);
	}
	public void selectQuestion() {
		answer = questions.get((int)(Math.random()*questions.size()));
		System.out.println(answer + " " + questions.size());
	}

	private void readFile() {

		try {
			BufferedReader breader = new BufferedReader(new FileReader(
					"question.txt"));
			String line;
			while ((line = breader.readLine()) != null) {
				questions.add(line);
			}
			breader.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public boolean applyWord(String str) {

		for(String answer : questions) {
			if(str.equals(answer) || str.length() > 4)
				return false;
		}

		try {
			BufferedWriter bwriter = new BufferedWriter(new FileWriter("question.txt", true)); 
			bwriter.write( str );
			bwriter.write( System.lineSeparator() ); 
			bwriter.close();

			} catch( Exception e) {
			}
		questions.add(str);
		return true;
	}
}
